package com.example.shop_2212.fgviewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.CodeEntityResult
import com.example.base.entity.PasswordEntityResult
import com.example.base.entity.UserInfoEntityResult
import com.example.base.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

/**
 * 我的页面的viewModel
 */
class MineViewModel :BaseViewModel(){
    //返回值
    val infoSuccess = MutableLiveData<UserInfoEntityResult>()
    val infoFail = MutableLiveData<String>()
    fun userInfo(){
        viewModelScope.launch {
            try {
                infoSuccess.value = apiServer.userInfo()
            }catch (e:Exception){
                infoFail.value = e.message
            }
        }
    }
    //修改密码返回值
    val pwdSuccess = MutableLiveData<PasswordEntityResult>()
    val pwdFail = MutableLiveData<String>()
    fun password(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,String>()
                map.put("account",account)
                map.put("captcha",captcha)
                map.put("password",password)
                pwdSuccess.value = apiServer.password(mapToBody(map))
            }catch (e:Exception){
                pwdFail.value = e.message
            }
        }
    }
    //返回值 --发送验证码
    val codeSuccess = MutableLiveData<CodeEntityResult>()
    val codeFail = MutableLiveData<String>()
    fun sendCode(phone:String){
        viewModelScope.launch {
            try {
                codeSuccess.value = apiServer.sendCode(phone)
            }catch (e:Exception){
                codeFail.value = e.message
            }
        }
    }
}